-
-
Notifications
You must be signed in to change notification settings - Fork 993
Add sameSite 'auto' support to match secure 'auto' pattern #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sameSite 'auto' support to match secure 'auto' pattern #1087
Conversation
bjohansebas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Overall, adding that option looks good to me, I think this would be good to have for v1.
By the way, I added more tests to better verify its behavior with different configurations
| it('should not set cookie when insecure', function (done) { | ||
| request(this.server) | ||
| .get('/') | ||
| .set('X-Secure', 'false') | ||
| .expect(shouldNotHaveHeader('Set-Cookie')) | ||
| .expect(200, 'false', done) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior here is interesting and cannot be changed since it would be a breaking change. However, because secure is set to true in the configuration, during cookie creation, which happens on this line
Line 161 in 2cd6561
| req.session.cookie = new Cookie(cookieOptions); |
Line 235 in 2cd6561
| if (req.session.cookie.secure && !issecure(req, trustProxy)) { |
f745109 to
6df617c
Compare
This PR adds support for
sameSite: 'auto'option that automatically sets the SameSite cookie attribute based on connection security, similar to the existingsecure: 'auto'feature.When the connection is secure (HTTPS), SameSite is set to 'None' to enable cross-site usage, and when insecure (HTTP), it's set to 'Lax' for better security. This solves real-world scenarios like SAML authentication where the connection security isn't known at configuration time.
The implementation follows the same pattern as
secure: 'auto', uses the existingissecure()function, and includes comprehensive test coverage with no breaking changes.Fixes #1081